<template>
  <h1>循环指令</h1>
  <ul>
    <li v-for="item in 5">Hello{{ item }}</li>
  </ul>
  <hr>
  <ul v-for="item in 3">
    <li>Hello{{ item }}</li>
    <li>Hello{{ item }}</li>
    <li>Hello{{ item }}</li>
  </ul>
  <ul v-for="p in productArr">
    <li>{{p}}</li>
  </ul>
  <hr>
  <table border="1">
    <tr>
      <th>商品编号</th>
      <th>商品名称</th>
      <th>商品价格</th>
      <th>商品数量</th>
      <th>操作</th>
    </tr>
    <tr v-for="(p,index) in productArr">
      <td>{{index+1}}</td>
      <td>{{p.title}}</td>
      <td>{{p.price}}</td>
      <td>{{p.num}}</td>
      <td><button @click="del(index)">删除</button></td>
    </tr>
  </table>
</template>

<script setup>
import {ref} from "vue";

//定义响应式数组
const productArr = ref([
  {title: "葡萄", price: 10, num: 100},
  {title: "苹果", price: 20, num: 200},
  {title: "西瓜", price: 30, num: 300},
  {title: "香蕉", price: 40, num: 400}
]);
//定义删除商品的方法
const del = (index) =>{
  if(confirm('您确认要删除该商品吗?')){
    productArr.value.splice(index,1);
  }
}
</script>

<style scoped>

</style>